using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VKASTBAT
    {
        public DataRow Convert2KASTBAT(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryKASTBAT(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["KBAMANDANT"] = " ".PadRight(2 ); 
         dwReturn["KBAKANR"] = " ".PadRight(8 ); 
         dwReturn["KBAKAPOS"] = " ".PadRight(4 ); 
         dwReturn["KBAPOS"] = " ".PadRight(0 ); 
         dwReturn["KBAWAKZ"] = " ".PadRight(1 ); 
         dwReturn["KBAKUNDNR"] = " ".PadRight(8 ); 
         dwReturn["KBAWERK"] = " ".PadRight(3 ); 
         dwReturn["KBAABT"] = " ".PadRight(3 ); 
         dwReturn["KBAART"] = " ".PadRight(20 ); 
         dwReturn["KBAVAR"] = " ".PadRight(3 ); 
         dwReturn["KBAFARBE"] = " ".PadRight(10 ); 
         dwReturn["KBADESS"] = " ".PadRight(10 ); 
         dwReturn["KBACOLORIT"] = " ".PadRight(10 ); 
         dwReturn["KBABESCHICH"] = " ".PadRight(10 ); 
         dwReturn["KBAZUSTAND"] = " ".PadRight(4 ); 
         dwReturn["KBAVERBR"] = " ".PadRight(0 ); 
         dwReturn["KBAMAEH"] = " ".PadRight(3 ); 
         dwReturn["KBAMAEHGRP"] = " ".PadRight(1 ); 
         dwReturn["KBAMAEHFKT"] = " ".PadRight(12 ); 
         dwReturn["KBAMEEH"] = " ".PadRight(3 ); 
         dwReturn["KBAMEEHGRP"] = " ".PadRight(1 ); 
         dwReturn["KBAMEEHFKT"] = " ".PadRight(12 ); 
         dwReturn["KBALO"] = " ".PadRight(2 ); 
         dwReturn["KBAABF"] = " ".PadRight(0 ); 
         dwReturn["KBANET"] = " ".PadRight(0 ); 
         dwReturn["KBABRUT"] = " ".PadRight(0 ); 
         dwReturn["KBAPREIS"] = " ".PadRight(0 ); 
         dwReturn["KBAKOSTEN"] = " ".PadRight(0 ); 
         dwReturn["KBAMGK1"] = " ".PadRight(0 ); 
         dwReturn["KBAMGK2"] = " ".PadRight(0 ); 
         dwReturn["KBAMGK3"] = " ".PadRight(0 ); 
         dwReturn["KBAMGK4"] = " ".PadRight(0 ); 
         dwReturn["KBAMGK5"] = " ".PadRight(0 ); 
         dwReturn["KBAARTAEND"] = " ".PadRight(1 ); 
         dwReturn["KBANETAEND"] = " ".PadRight(1 ); 
         dwReturn["KBAABFAEND"] = " ".PadRight(1 ); 
         dwReturn["KBAPRAEND"] = " ".PadRight(1 ); 
         dwReturn["KBABRT"] = " ".PadRight(0 ); 
         dwReturn["KBAFAK"] = " ".PadRight(0 ); 
         dwReturn["KBAPART"] = " ".PadRight(5 ); 
         dwReturn["KBASTUEART"] = " ".PadRight(3 ); 
         dwReturn["KBAFILLER"] = " ".PadRight(108 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapKASTBAT(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveKASTBAT(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryKASTBAT(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapKASTBAT(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertKASTBAT(dwLocal, ITXDBVERSION);
            }
            UpdateKASTBAT(dwLocal);
        }
        public void InsertKASTBAT(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO KASTBAT(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryKASTBAT(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM KASTBAT WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM KASTBAT  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "KASTBAT";
            Return = Result;
            return Return;
        }
        public void DeleteKASTBAT(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM KASTBAT   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateKASTBAT(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE KASTBAT SET "
                    +"KBAMANDANT= :KBAMANDANT,"
                    +"KBAKANR= :KBAKANR,"
                    +"KBAKAPOS= :KBAKAPOS,"
                    +"KBAPOS= :KBAPOS,"
                    +"KBAWAKZ= :KBAWAKZ,"
                    +"KBAKUNDNR= :KBAKUNDNR,"
                    +"KBAWERK= :KBAWERK,"
                    +"KBAABT= :KBAABT,"
                    +"KBAART= :KBAART,"
                    +"KBAVAR= :KBAVAR,"
                    +"KBAFARBE= :KBAFARBE,"
                    +"KBADESS= :KBADESS,"
                    +"KBACOLORIT= :KBACOLORIT,"
                    +"KBABESCHICH= :KBABESCHICH,"
                    +"KBAZUSTAND= :KBAZUSTAND,"
                    +"KBAVERBR= :KBAVERBR,"
                    +"KBAMAEH= :KBAMAEH,"
                    +"KBAMAEHGRP= :KBAMAEHGRP,"
                    +"KBAMAEHFKT= :KBAMAEHFKT,"
                    +"KBAMEEH= :KBAMEEH,"
                    +"KBAMEEHGRP= :KBAMEEHGRP,"
                    +"KBAMEEHFKT= :KBAMEEHFKT,"
                    +"KBALO= :KBALO,"
                    +"KBAABF= :KBAABF,"
                    +"KBANET= :KBANET,"
                    +"KBABRUT= :KBABRUT,"
                    +"KBAPREIS= :KBAPREIS,"
                    +"KBAKOSTEN= :KBAKOSTEN,"
                    +"KBAMGK1= :KBAMGK1,"
                    +"KBAMGK2= :KBAMGK2,"
                    +"KBAMGK3= :KBAMGK3,"
                    +"KBAMGK4= :KBAMGK4,"
                    +"KBAMGK5= :KBAMGK5,"
                    +"KBAARTAEND= :KBAARTAEND,"
                    +"KBANETAEND= :KBANETAEND,"
                    +"KBAABFAEND= :KBAABFAEND,"
                    +"KBAPRAEND= :KBAPRAEND,"
                    +"KBABRT= :KBABRT,"
                    +"KBAFAK= :KBAFAK,"
                    +"KBAPART= :KBAPART,"
                    +"KBASTUEART= :KBASTUEART,"
                    +"KBAFILLER= :KBAFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("KBAMANDANT", OracleType.NVarChar).Value = dwData["KBAMANDANT"].ToString(); 
          or1.Parameters.Add("KBAKANR", OracleType.NVarChar).Value = dwData["KBAKANR"].ToString(); 
          or1.Parameters.Add("KBAKAPOS", OracleType.NVarChar).Value = dwData["KBAKAPOS"].ToString(); 
          or1.Parameters.Add("KBAPOS", OracleType.NVarChar).Value = dwData["KBAPOS"].ToString(); 
          or1.Parameters.Add("KBAWAKZ", OracleType.NVarChar).Value = dwData["KBAWAKZ"].ToString(); 
          or1.Parameters.Add("KBAKUNDNR", OracleType.NVarChar).Value = dwData["KBAKUNDNR"].ToString(); 
          or1.Parameters.Add("KBAWERK", OracleType.NVarChar).Value = dwData["KBAWERK"].ToString(); 
          or1.Parameters.Add("KBAABT", OracleType.NVarChar).Value = dwData["KBAABT"].ToString(); 
          or1.Parameters.Add("KBAART", OracleType.NVarChar).Value = dwData["KBAART"].ToString(); 
          or1.Parameters.Add("KBAVAR", OracleType.NVarChar).Value = dwData["KBAVAR"].ToString(); 
          or1.Parameters.Add("KBAFARBE", OracleType.NVarChar).Value = dwData["KBAFARBE"].ToString(); 
          or1.Parameters.Add("KBADESS", OracleType.NVarChar).Value = dwData["KBADESS"].ToString(); 
          or1.Parameters.Add("KBACOLORIT", OracleType.NVarChar).Value = dwData["KBACOLORIT"].ToString(); 
          or1.Parameters.Add("KBABESCHICH", OracleType.NVarChar).Value = dwData["KBABESCHICH"].ToString(); 
          or1.Parameters.Add("KBAZUSTAND", OracleType.NVarChar).Value = dwData["KBAZUSTAND"].ToString(); 
          or1.Parameters.Add("KBAVERBR", OracleType.NVarChar).Value = dwData["KBAVERBR"].ToString(); 
          or1.Parameters.Add("KBAMAEH", OracleType.NVarChar).Value = dwData["KBAMAEH"].ToString(); 
          or1.Parameters.Add("KBAMAEHGRP", OracleType.NVarChar).Value = dwData["KBAMAEHGRP"].ToString(); 
          or1.Parameters.Add("KBAMAEHFKT", OracleType.NVarChar).Value = dwData["KBAMAEHFKT"].ToString(); 
          or1.Parameters.Add("KBAMEEH", OracleType.NVarChar).Value = dwData["KBAMEEH"].ToString(); 
          or1.Parameters.Add("KBAMEEHGRP", OracleType.NVarChar).Value = dwData["KBAMEEHGRP"].ToString(); 
          or1.Parameters.Add("KBAMEEHFKT", OracleType.NVarChar).Value = dwData["KBAMEEHFKT"].ToString(); 
          or1.Parameters.Add("KBALO", OracleType.NVarChar).Value = dwData["KBALO"].ToString(); 
          or1.Parameters.Add("KBAABF", OracleType.NVarChar).Value = dwData["KBAABF"].ToString(); 
          or1.Parameters.Add("KBANET", OracleType.NVarChar).Value = dwData["KBANET"].ToString(); 
          or1.Parameters.Add("KBABRUT", OracleType.NVarChar).Value = dwData["KBABRUT"].ToString(); 
          or1.Parameters.Add("KBAPREIS", OracleType.NVarChar).Value = dwData["KBAPREIS"].ToString(); 
          or1.Parameters.Add("KBAKOSTEN", OracleType.NVarChar).Value = dwData["KBAKOSTEN"].ToString(); 
          or1.Parameters.Add("KBAMGK1", OracleType.NVarChar).Value = dwData["KBAMGK1"].ToString(); 
          or1.Parameters.Add("KBAMGK2", OracleType.NVarChar).Value = dwData["KBAMGK2"].ToString(); 
          or1.Parameters.Add("KBAMGK3", OracleType.NVarChar).Value = dwData["KBAMGK3"].ToString(); 
          or1.Parameters.Add("KBAMGK4", OracleType.NVarChar).Value = dwData["KBAMGK4"].ToString(); 
          or1.Parameters.Add("KBAMGK5", OracleType.NVarChar).Value = dwData["KBAMGK5"].ToString(); 
          or1.Parameters.Add("KBAARTAEND", OracleType.NVarChar).Value = dwData["KBAARTAEND"].ToString(); 
          or1.Parameters.Add("KBANETAEND", OracleType.NVarChar).Value = dwData["KBANETAEND"].ToString(); 
          or1.Parameters.Add("KBAABFAEND", OracleType.NVarChar).Value = dwData["KBAABFAEND"].ToString(); 
          or1.Parameters.Add("KBAPRAEND", OracleType.NVarChar).Value = dwData["KBAPRAEND"].ToString(); 
          or1.Parameters.Add("KBABRT", OracleType.NVarChar).Value = dwData["KBABRT"].ToString(); 
          or1.Parameters.Add("KBAFAK", OracleType.NVarChar).Value = dwData["KBAFAK"].ToString(); 
          or1.Parameters.Add("KBAPART", OracleType.NVarChar).Value = dwData["KBAPART"].ToString(); 
          or1.Parameters.Add("KBASTUEART", OracleType.NVarChar).Value = dwData["KBASTUEART"].ToString(); 
          or1.Parameters.Add("KBAFILLER", OracleType.NVarChar).Value = dwData["KBAFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
